﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Security.Principal;
using Facebook;
using System.Web.Security;

namespace B10
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{handler}.ashx");
            routes.IgnoreRoute("captcha.ashx");

            routes.MapRoute(
               "UserComments",
               "profil/{id}/{name}/komentarze/{page}",
               new { controller = "PublicProfile", action = "Comments", id = "",name="", page = "" }
               );

            routes.MapRoute(
               "UserPageList",
               "profil/{id}/{name}/strony/{page}",
               new { controller = "PublicProfile", action = "PageList", id = "",name="", page = "" }
               );

            routes.MapRoute(
                "PublicProfile",
                "profil/{id}/{name}",
                new { controller = "PublicProfile", action = "Index",name="", id = "" }
                );

            routes.MapRoute(
                "Profile",
                "moje-konto",
                new { controller = "Profile", action = "Index" }
                );

            routes.MapRoute(
                "ProfilePageList",
                "moje-konto/strony",
                new { controller = "Profile", action = "PageList" }
                );

            routes.MapRoute(
                "ProfileSettings",
                "moje-konto/ustawienia",
                new { controller = "Profile", action = "AdsenseSettings" }
                );

            routes.MapRoute(
                "Categories",
                "kategoria/{categoryName},{category},{page},{sort}",
                new
                {
                    controller = "Home",
                    action = "Categories",
                    categoryName = "",
                    category = "",
                    page = "",
                    sort = "2"
                },
                new { page = @"\d*" }
                );

            //routes.MapRoute(
            //    "RssCategories",
            //    "rss/kategoria/{categoryName},{category},{sort}",
            //    new
            //    {
            //        controller = "Rss",
            //        action = "Categories",
            //        categoryName = "Sztuka i Literatura",
            //        category = "0",
            //        sort = "2"
            //    },
            //    new { category = @"\d+" }
            //    );

            routes.MapRoute(
                   "PageList1",
                   "najnowsze/{page}",
                       new { controller = "Home", action = "Index", page = "" },
                       new { page = @"\d*", sort = "0" }
                   );

            routes.MapRoute(
                   "PageList1A",
                   "najnowsze/{page}",
                       new { controller = "Home", action = "Index", page = "", sort = "0" },
                       new { page = @"\d*", sort = "0" }
                   );

            routes.MapRoute(
                "Rss1",
                "rss/najnowsze",
                    new { controller = "Rss", action = "Index", sort = "0" },
                    new { sort = "0" }
                    );

            routes.MapRoute(
                   "PageList2",
                   "top-dnia/{page}",
                       new { controller = "Home", action = "Index", page = "" },
                       new { page = @"\d*", sort = "1" }
                   );

            routes.MapRoute(
                  "PageList2A",
                  "top-dnia/{page}",
                      new { controller = "Home", action = "Index", page = "", sort = "1" },
                      new { page = @"\d*", sort = "1" }
                  );

            routes.MapRoute(
                "Rss2",
                "rss/top-dnia",
                    new { controller = "Rss", action = "Index", sort = "1" },
                    new { sort = "1" }
                    );

            //routes.MapRoute(
            //       "PageList3",
            //       "top-tygodnia/{page}",
            //           new { controller = "Home", action = "Index", page = "" },
            //           new { page = @"\d*", sort = "2" }
            //       );

            //routes.MapRoute(
            //      "PageList3A",
            //      "top-tygodnia/{page}",
            //          new { controller = "Home", action = "Index", page = "", sort = "2" },
            //          new { page = @"\d*", sort = "2" }
            //      );

            //routes.MapRoute(
            //   "Rss3",
            //   "rss/top-tygodnia",
            //       new { controller = "Rss", action = "Index", sort = "2" }
            //       );

            routes.MapRoute(
                   "PageList4",
                   "top-miesiaca/{page}",
                       new { controller = "Home", action = "Index", page = "" },
                       new { page = @"\d*", sort = "3" }
                   );

            routes.MapRoute(
                   "PageList4A",
                   "top-miesiaca/{page}",
                       new { controller = "Home", action = "Index", page = "", sort = "3" },
                       new { page = @"\d*", sort = "3" }
                   );

            routes.MapRoute(
               "Rss4",
               "rss/top-miesiaca",
                   new { controller = "Rss", action = "Index", sort = "3" },
                   new { sort = "3" }
                   );

            routes.MapRoute(
                   "PageList5",
                   "najpopularniejsze/{page}",
                       new { controller = "Home", action = "Index", page = "" },
                       new { page = @"\d*", sort = "4" }
                   );

            routes.MapRoute(
                   "PageList5A",
                   "najpopularniejsze/{page}",
                       new { controller = "Home", action = "Index", page = "", sort = "4" },
                       new { page = @"\d*", sort = "4" }
                   );

            routes.MapRoute(
              "Rss5",
              "rss/najpopularniejsze",
                  new { controller = "Rss", action = "Index", sort = "4" },
                  new { sort = "4" }
                  );

            routes.MapRoute(
                  "PageList",
                  "{page}",
                      new { controller = "Home", action = "Index", page = "" },
                      new { page = @"\d*", sort = "2" }
                  );

            routes.MapRoute(
                 "PageListA",
                 "{page}",
                     new { controller = "Home", action = "Index", page = "", sort = "2" },
                     new { page = @"\d*", sort = "2" }
                 );

            routes.MapRoute(
              "Rss3",
              "rss",
                  new { controller = "Rss", action = "Index", sort = "2" },
                  new { sort = "2" }
                  );

            routes.MapRoute(
             "UserPage",
             "strona/{pageUrl},{id}",
             new { controller = "Home", action = "Page", pageUrl = "", id = "" }
           );

            routes.MapRoute(
                "HelpStart",
                "jak-to-dziala",
                new { controller = "Help", action = "Index" }
                );

            routes.MapRoute(
               "Start",
               "jak-zaczac",
               new { controller = "Help", action = "Start" }
               );

            routes.MapRoute(
                "Earn",
                "ile-mozna-zarobic",
                new { controller = "Help", action = "Earn" }
                );

            routes.MapRoute(
            "Promote",
            "jak-promowac-strony",
            new { controller = "Help", action = "Promote" }
            );

            routes.MapRoute(
            "StartAdsense",
            "co-to-jest-google-adsense",
            new { controller = "Help", action = "StartAdsense" }
            );

            routes.MapRoute(
            "FAQ",
            "faq",
            new { controller = "Help", action = "Faq" }
            );

            routes.MapRoute(
            "Terms",
            "regulamin",
            new { controller = "Help", action = "Terms" }
            );

            routes.MapRoute(
            "Contact",
            "kontakt",
            new { controller = "Help", action = "Contact" }
            );



            routes.MapRoute(
                "UserList",
                "autorzy",
                new { controller = "Authors", action = "Index" }
                );

            routes.MapRoute(
                "Search",
                "szukaj",
                new { controller = "Search", action = "Index" }
                );

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
                );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                if (FacebookConnect.IsConnected)
                {
                    string name = FacebookConnect.GetUserName();
                    if (string.IsNullOrEmpty(name))
                        return;
                    // Create an Identity object
                    System.Security.Principal.GenericIdentity id =
                        new GenericIdentity(name, "facebook");

                    // This principal will flow throughout the request.
                    System.Security.Principal.GenericPrincipal principal =
                        new GenericPrincipal(id, null);
                    // Attach the new principal object to the current HttpContext object
                    Context.User = principal;
                }
            }
        }

    }
}